import { RequestConfig } from "./requests";
import createRequest from "./requests";
/**
 * Response 泛型接口约束后端返回数据的格式
 *
 * 不同的系统约定不同，所以在不同项目中可以是不一样。
 *
 */
export interface Response<T = any> {
  success: boolean;
  message?: string;
  object?: T;
  query?: {
    count: number;
    limit: number;
    offset: number;
    page: number;
    results: Array<T>;
  };
  errors?: {
    [props: string]: string[];
  };
}
// 使用特殊环境变量替代 baseURL
const baseURL = import.meta.env.VITE_BASE_URL ?? "/";
// createRequest 生成一个泛型函数
export const customRequest = createRequest({ baseURL, timeout: 15000 });

export const request = <T = any>(url: string, options: RequestConfig = {}) =>
  customRequest<Response<T>>(url, options);
